<?php
/**
 * Created by PhpStorm.
 * @description:
 * @time:
 * @Author: yfl
 * @QQ 554665488
 * Date: 2018-5-28
 * Time: 15:08
 */
$server = new swoole_server('127.0.0.1',9501,SWOOLE_PROCESS,SWOOLE_SOCK_TCP);
//配置eof协议分包
//$server->set([
//
//    'worker_num' => 2, //设置进程
//    'open_eof_check' => true, //打开EOF检测
//    'package_eof' => "\r\n", //设置EOF
////    'open_eof_split'=>true //自动进行边界分割
//]);
//固定包头跟包体协议
$server->set([
    'open_length_check' => true,
    'package_length_type'=>'N',
    'package_length_offset'=>0, //计算总长度
    'package_body_offset'=>4,//包体位置
    'package_max_length'=>1024*1024 //总的请求数据大小字节为单位
]);
//监听连接进入事件,有客户端连接进来的时候会触发
$server->on('connect', function ($serv, $fd) {

});


//监听数据接收事件,server接收到客户端的数据后，worker进程内触发该回调
$server->on('receive', function ($serv, $fd, $from_id, $data) {
    $len=unpack('N',$data);

//    var_dump($len);
    $data=substr($data,-$len[1]);
//    var_dump( $data);
    $serv->send($fd, "服务器给你发送消息了:".$data.PHP_EOL);
//    for ($i=0;$i<10;$i++){
//        $serv->send($fd, "服务器给你发送消息了:".$data.PHP_EOL);
//    }


});


//监听连接关闭事件,客服端关闭，或者服务器主动关闭
$server->on('close', function ($serv, $fd) {
});
//启动服务器
$server->start();